package com.zzjson.design.adapter.loginadapter.v2;

import com.zzjson.design.adapter.loginadapter.ResultMsg;
import com.zzjson.design.adapter.loginadapter.v1.service.SiginService;

/**
 * <p>****************************************************************************</p>
 * <p><b>Copyright © 2010-2019 shuncom team All Rights Reserved<b></p>
 * <ul style="margin:15px;">
 * <li>Description : cn.zzjson.design.adapter.loginadapter.v2</li>
 * <li>Version     : 1.0.0</li>
 * <li>Creation    : 2019年03月28日</li>
 * <li>@author     : zzy0_0</li>
 * </ul>
 * <p>****************************************************************************</p>
 */
public class PasswordForThirdAdapter extends SiginService implements IPasswordForThird {

    @Override
    public ResultMsg loginForQQ(String id) {
        return processLogin(id, LoginForQQAdapter.class);
    }


    @Override
    public ResultMsg loginForWechat(String id) {
        return processLogin(id, LoginForWechatAdapter.class);
    }

    private ResultMsg processLogin(String key, Class<? extends LoginAdapter> clazz) {
        try {
            //适配器不一定要实现接口
            LoginAdapter adapter = clazz.newInstance();

            //判断传过来的适配器是否能处理指定的逻辑
            if (adapter.support(adapter)) {
                return adapter.login(key);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}